import UIKit
//基本运算符
var str = "Hello, playground"

var (x, y) = (1, 2)
/*
if x = y {
    // 这是不合法的, 因为 x = y 并不会返回任何值。
}
 */

//swift中字符串拼接
let helloWorldStr = "hello," + "world"


/*
 Swift 同时也提供两个等价运算符（ ===  和 !== ），你可以使用它们来判断两个   **对象**的引用是否相同  只能用于类和结构体？
 */
class Animal{
    var name:String?
    var weight:Double?
}

let dog = Animal()
dog.name = "dog"
dog.weight = 40.0

if let dogName = dog.name, let dogWeight = dog.weight {
    print("dog name is \(dogName) , weight is \(dogWeight)")
    
}else{
    print("error")
}

let whiteDog = dog
whiteDog.name = "littleWhite"
whiteDog.weight = 40

if whiteDog === dog {
    print("whiteDog name is \(whiteDog.name!) . weight is \(whiteDog.weight!)")
}else{
    print("littleWhite is not exist")
}

//元组也可比较
//  Int 和 String 都可以用来比较大小 bool不行
"a" < "b"
(1, "zebra") < (2, "apple")
(3, "apple") < (3, "bird")
(4, "dog") == (4, "dog")

//三元运算符
let contentHeight = 40
let hasHeader = true
let rowHeight = contentHeight + (hasHeader ? 50 : 20)

//展开写是
if hasHeader {
//    因为没用到这个值所以”_“代替
    _ = contentHeight + 50
}else{
    _ = contentHeight + 20
}

//合并空值运算符
//合并空值运算符 （ a ?? b ）如果可选项 a  有值则展开，如果没有值，是 nil  ，则返回默认值 b 。表达式 a 必须是一个可选类型。表达式 b  必须与 a  的储存类型相同。
let defaultColorName = "red"
var userDefinedColorName: String? // defaults to nil
var colorNameToUse = userDefinedColorName ?? defaultColorName
//如果userDefinedColorName 非nil 则展开  如果为nil  则取值defaultColorName
//⚠️ userDefinedColorName ?? defaultColorName  两个值类型必须是同一类型

//区间运算符
//1.闭区间运算符
//"a...b"包含a 和 b区间的所有值
for index in 1...5 {
    print("\(index)")
}
//2.半开区间运算符
//”0..<7“ 0到6  不包含7
let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..<count {
    print("Person \(i + 1) is called \(names[i])")
}

//单侧区间
// ”2...“ 从第2元素到数组结束
//”...2“  从第0元素到数组第2元素
for name in names[2...] {
    print(name)
}
 
for name in names[...2] {
    print(name)
}


//混合逻辑运算
let enteredDoorCode = true
let passedRetinaScan = false
let hasDoorKey = false
let knowsOverridePassword = true

if (enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword {
    print("Welcome!")
} else {
    print("ACCESS DENIED")
}

//Swift 语言中逻辑运算符 && 和 || 是左相关的
//这意味着多个逻辑运算符组合的表达式会首先计算最左边的子表达式。

/*
 可简写为为如下：
 
 if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword {
     print("Welcome!")
 } else {
     print("ACCESS DENIED")
 }
 
 */
